#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
inline int read(){
    int u=0,v=1;
    char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-') v=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        u=(u<<1)+(u<<3)+ch-'0';
        ch=getchar();
    }
    return u*v;
}
void print(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9) print(x/10);
    putchar(x%10+'0');
}
const int N=1e5+10;
int a[N],a0[N],n,m,c[N],T,x[4];
bitset<1001> d[1001];
vector<int> v,winner;
string st;
int main(){
    srand(time(0));
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=n;i++) a0[i]=read();
    for(int i=1;i<=m;i++) c[i]=read();
    int K=ceil(log(n*1.0)/log(2.0));
    for(int i=1;i<=K;i++){
        cin>>st;
        for(int j=0;j<st.size();j++){
            d[i][j+1]=st[j]-'0';
        }
    }
    T=read();
    while(T--){
        int ans=0;
        x[0]=read(),x[1]=read(),x[2]=read(),x[3]=read();
        /*for(int i=1;i<=n;i++){
            a[i]=a0[i]^x[i%4];
        }
        for(int i=1;i<=m;i++){
            v.clear(),winner.clear();
            for(int j=1;j<c[i];j++){
                v.push_back(a[j]);
            }
            int r=0;
            while(v.size()>1){
                r++;
                int g=0;
                for(int j=0;j<v.size();j+=2){
                    g++;
                    if(d[r][g]){
                        int p=max(v[j],v[j+1]);
                    }
                }
            }
        }*/
        cout<<rand()<<endl;
    }
    return 0;
}